"""A collection of viewing tools for itk and numpy images"""
import itk
from . import query 
from ..browse import a3d
import numpy as na
import matplotlib.pyplot as pyplot
def view2DImage(img, wait=False, itkMode = True,
                saveName='',**unused ):
    """view a 2D image.
    
    img: an itk Image or a numpy array
    intype: deprecated
    itkMode = Flag to specify whether img is itk or numpy object. Shouldn't need
    this
    
    savename: ???
    
    """
    try:
        if( itkMode ):
            itype = query.getImageType(img)
            dim = query.getImgDim(img)
            intype = itk.Image[itype,dim]
            range = itk.range(img)
            array = na.array(itk.PyBuffer[intype].GetArrayFromImage(img))
        else:
            array = img
            #range = (na.min(array),na.max(array))
            #array = (array-range[0])*255.0/(range[1]-range[0])
            #print type(array),array.shape
            #i=biu.ArrayToPilImageStraight(array.astype(na.uint8))
        im = pyplot.imshow(array)
        pyplot.show()
        if( wait):
            raw_input('continue')
    except Exception, error:
        print "failed in view2DImage()", error
def view3DImage(imgs,masks=[], scale=[1], 
                colorFile=None,labelFile=None,**args ):
    """view a collection of 3D images
       imgs: a list of images
       masks:  a list of binary masks matching each provided image. Optional
       scale: an overall scaling factor to display the images to
       """
    try:
        
        #inIType = itk.Image[intype,2]
        scl = query.get_itkVector(imgs[0].GetSpacing())
        print scl
        arrays = []
        print "get image data"
        for i in range(len(imgs)):
            img = imgs[i]
            try:
                s = scale[i]
            except:
                s = scale[0]
            print "scale=",s
            itype = query.getImageType(img)
            dim = query.getImgDim(img)
            array = na.array(itk.PyBuffer[itype].GetArrayFromImage(img))*s
            print "max=",array.max()
            arrays.append(array)
        marrays = []
        print "get mask data"
        for mask in masks:       
            array = na.array(itk.PyBuffer[itype].GetArrayFromImage(mask))
            marrays.append(array)
        print "view data"
        a3d.call(data=arrays, gFactor=scale, mask=marrays, 
                 scls = scl,colorFile=colorFile,
                 labelFile=labelFile,**args) 
    except Exception, error:
        print "failed in view3DImage()", error 
